package com.cnd.scm.code.generator.provider;

import com.cnd.scm.jdbcprocessor.constant.DataBaseType;
import com.cnd.scm.jdbcprocessor.helper.SqlHelper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class SchemaProviderFactory {

	@Autowired
	private SqlHelper sqlHelper;
	
/*	@Autowired
	@Qualifier("mySQLSchemaProvider")
	private IDbSchemaProvider mySQLSchemaProvider;
	
	@Autowired
	@Qualifier("sqlSchemaProvider")
	private IDbSchemaProvider sqlSchemaProvider;
*/

    /*动态注入 此写法更优雅*/
	Map<String,IDbSchemaProvider> schemaProvidersMap = new HashMap<String,IDbSchemaProvider>();
	public SchemaProviderFactory(List<IDbSchemaProvider> schemaProviders){
        for (IDbSchemaProvider schemaProvider : schemaProviders) {
            schemaProvidersMap.put(schemaProvider.dataBaseType().toString(),schemaProvider);
        }
    }

    public IDbSchemaProvider createDbSchemaProvider() {
        IDbSchemaProvider schemaProvider = null;
        DataBaseType dataBaseType = null;
        try {
            dataBaseType = sqlHelper.getDataBaseType();
        } catch (Exception e) {
            e.printStackTrace();
        }
        schemaProvider = schemaProvidersMap.get(dataBaseType.toString());
        return schemaProvider;

    }
	
/*
	public IDbSchemaProvider createDbSchemaProvider() {
		IDbSchemaProvider schemaProvider = null;
		DataBaseType dataBaseType = null;
		try {
			dataBaseType = sqlHelper.getDataBaseType();
		} catch (Exception e) {
			e.printStackTrace();
		}
		switch(dataBaseType){
			case MYSQL:
				schemaProvider = mySQLSchemaProvider;
				break;
			case SQLSERVER:
				schemaProvider =sqlSchemaProvider;
				break;
			default:
				break;
		}
		return schemaProvider;
	}
*/

}
